//
//  AddTabbarView.swift
//  微博swift
//
//  Created by CrateMac0 on 16/3/20.
//  Copyright © 2016年 Users. All rights reserved.
//

import UIKit
let tabBatItemCount = 5
class AddTabbarView: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        //print("layoutSubviews")//会打印两次
        print("subviews:\(subviews)")//在控制器中已经把本类添加tabBar，打印出来会有很多东西，需要找到添加到里面自定义UIButton,就是UITabBarButton
        let width = frame.width / CGFloat(tabBatItemCount)
        var count = 0
        for tabbar in subviews {
            let lei = NSClassFromString("UITabBarButton")
            if tabbar.isKindOfClass(lei!) {
                tabbar.frame = CGRect(x: (CGFloat(count) * width), y: 0, width: width, height: frame.height)
                count += (count == 1 ? 2:1)
            }
            addBtn.frame = CGRect(x: 2.0 * width, y: 0, width: width, height: frame.height  )
        }
    }
    
    //MARK: - 懒加载 
    private lazy var addBtn: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        self.addSubview(button)
        return button
    }()
}
